import Vue from 'vue'
import Vuex from 'vuex'

import { setItem, getItem, removeItem } from '@/utils/storage.js'
import { updateUserPhoto } from '../api/user'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    // 保存公共数据
    // 从localStorage中去取
    user: getItem('user'),
    photo: getItem('photo')// 用户头像
  },

  mutations: {
    // 更新用户token
    updateToken (state, newToken) {
      state.user.token = newToken
      setItem('user', state.user)
    },
    // 设置user
    setUser (state, obj) {
      state.user = obj

      // 信息持久化
      setItem('user', obj)
    },
    delUser (state) {
      state.user = null
      removeItem('user')
    },

    // 修改头像
    updateUserPhoto (state, photo) {
      state.photo = photo
      setItem('photo', photo)
    }
  },
  actions: {
  },
  modules: {
  }
})
